<?php
class ModelSaleOrder extends Model {
	
	public function getexportOrders($data = array()) {
		
		
		ini_set('display_errors', 1);
		ini_set('log_errors', 1);
		error_reporting(E_ALL);
		ini_set("memory_limit","-1");
		set_time_limit(0);
        header("Content-type: text/html; charset=utf-8");
		require_once DIR_SYSTEM . 'phpexcel/Classes/PHPExcel.php';
		
		$allowedExts = array("xls", "xlsx", "csv");
		$temp = explode(".", $_FILES["file"]["name"]);
		$extension = end($temp);     // 获取文件后缀名
		// var_dump($extension);
		// var_dump($_FILES);
		// echo exec('whoami');
		// echo '</br>';
		$filePath = '';
		if ($_FILES["file"]["size"] > 4096000){
			echo "文件大小不要超过4M";
			die();
		}
		if (($_FILES["file"]["size"] < 4096000)   // 小于 2 MB  2319018
			&& in_array($extension, $allowedExts)
		) {
			if ($_FILES["file"]["error"] > 0) {
				echo "错误：: " . $_FILES["file"]["error"] . "<br>";
			} else {
				$path=$_SERVER['DOCUMENT_ROOT']."/ueditor/php/upload/image"; //上传路径   
				// 判断当期目录下的 upload 目录是否存在该文件
				// 如果没有 upload 目录，你需要创建它，upload 目录权限为 777
				if (file_exists($path."/" . $_FILES["file"]["name"])) {
					echo $_FILES["file"]["name"] . " 文件已经存在。 ";
				} else {
					//重命名文件名
					$filename = date('YmdHis' . rand(1000, 9999)) . '.xls';
					// 如果 upload 目录不存在该文件则将文件上传到 upload 目录下
					move_uploaded_file($_FILES["file"]["tmp_name"], $path."/" . $filename);
					$filePath = $path."/" . $filename;
				}
			}
		} else {
			echo "非法的文件格式";
			die();
		}
		
		require_once  DIR_SYSTEM . 'phpexcel/Classes/PHPExcel/IOFactory.php';
		require_once  DIR_SYSTEM . 'phpexcel/Classes/PHPExcel/Reader/Excel2007.php';
		 
		$objReader = PHPExcel_IOFactory::createReader('Excel2007');/*Excel5 for 2003 excel2007 for 2007*/
		$objPHPExcel = $objReader->load($filePath); //Excel 路径
		// $sheet = $objPHPExcel->getSheet(0);
		// $highestRow = $sheet->getHighestRow(); // 取得总行数
		// $highestColumn = $sheet->getHighestColumn(); // 取得总列数
		
		/*方法二【推荐】*/
		$objWorksheet = $objPHPExcel->getActiveSheet();        
		$highestRow = $objWorksheet->getHighestRow();   // 取得总行数     
		$highestColumn = $objWorksheet->getHighestColumn();        
		$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);//总列数
				$check=array(
					0 =>'广告专员',1 =>'所属业务部门' ,2 =>'域名' ,3 => '订单号' , 4 => '商户订单号' , 5 =>'运单号',6 =>'姓名' , 7 =>'产品名' , 8 =>'内部名', 9 =>'面单名' ,10 =>'总价' ,11 =>'符号',12 =>'属性' ,13 =>'SKU' ,14 =>'电话', 15 =>'邮箱', 16 =>'邮编' , 17 =>'送货地址' , 18 =>'省',19 =>'市' ,20 =>'区' ,21 =>'购买产品数量' ,22 =>'单价', 23 =>'付款方式', 24 =>'来源',25 =>'留言',26 =>'同步时间',27 =>'下单时间' ,28 =>'到货时间' ,29 =>'上线时间',30 =>'签收时间', 31 =>'仓库', 32 =>'订单状态',33 =>'无效原因' ,34 =>'取消原因', 35 =>'验证码状态',36 =>'发货日期',37 =>'物流名称' ,38 =>'物流状态' ,39 =>'结款状态' ,40 =>'重量', 41 =>'IP' ,42 =>'重复信息' 
				);

				$check_one=array(
					0 =>'订单号ID',1 =>'下单时间' ,2 =>'部门' ,3 =>'优化师' ,4 =>'站点域名' ,5 =>'验证码状态' ,6 =>'收货姓名',7 =>'国家' ,8 =>'省份' ,9 =>'市' ,10 =>'区' ,11 =>'街道' ,12 =>'邮编' ,13 =>'地址' ,14 =>'电话' ,15 =>'邮箱' ,16 =>'备注' ,17 =>'投放地区' ,18 =>'货币' ,19 =>'订单总额',20 =>'人民币' ,21 =>'sku数量',22 =>'产品1-erpID' ,23 =>'产品1-SKU' ,24 =>'产品1-内部名' ,25 =>'产品1-外文名' ,26 =>'产品1-面单名' ,27 =>'产品1-单价' ,28 =>'产品1-数量',29 =>'产品1-属性1外文名',30 =>'产品1-属性1内文名' ,31 =>'产品1-属性1 id' ,32 =>'产品1-属性1属性值外文名' ,33 =>'产品1-属性1属性值内文名',34 =>'产品1-属性1属性值 id' ,35 =>'产品1-属性2外文名' ,36 =>'产品1-属性2内文名',37 =>'产品1-属性2 id' ,38 =>'产品1-属性2属性值外文名' ,39 =>'产品1-属性2属性值内文名',40 =>'产品1-属性2属性值 id' 
				);
			
			    $check_two=array(
				    0 =>'地区',1 =>'广告专员',2 =>'所属业务部门',3 =>'域名',4 =>'订单号',5 =>'商户订单号',6 =>'运单号',7 =>'姓名',8 =>'内部名',9 =>'面单名',10 =>'总价',11 =>'人民币',12 =>'符号',13 =>'属性',14 =>'SKU',15 =>'电话',16 =>'邮箱',17 =>'邮编',18 =>'送货地址',19 =>'省',20 =>'市',21 =>'区',22 =>'留言',23 =>'购买产品数量',24 =>'单价',25 =>'付款方式',26 =>'来源',27 =>'备注',28 =>'同步时间',29 =>'下单时间',30 =>'到货时间',31 =>'上线时间',32 =>'签收时间',33 =>'仓库',34 =>'订单状态',35 =>'分类',36 =>'无效原因',37 =>'取消原因',38 =>'验证码状态',39 =>'发货日期',40 =>'物流名称',41 =>'物流状态',42 =>'结款状态',43 =>'重量',44 =>'标签'
				);
	    $ordenum=0;
		for ($row = 2;$row <= $highestRow;$row++){
			$strs=array();
			//注意highestColumnIndex的列数索引从0开始
			for ($col = 0;$col < $highestColumnIndex;$col++)            {
				$strs[$col] =(string)$objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
			}
			
			for ($col = 0;$col < $highestColumnIndex;$col++)  {
				$val=(string)$objWorksheet->getCellByColumnAndRow($col, 1)->getValue();
				
				$strsfirst[$col] =(string)$val;
				$strsfirsts=array_slice($strsfirst,0,41);
			}
		      // var_dump($strsfirst);die;
			  // var_dump($strs);
			
				if($strsfirst[4]=='网站地址'&&$strsfirst[13]!='地区'){
					$order_query = $this->db->query("SELECT order_id FROM " . DB_PREFIX . "order o WHERE o.order_number = '" . (int)$strs[0] . "'");
					if(!$order_query->num_rows){
						$this->db->query("INSERT INTO `oc_order` ( `order_number`,`store_url`,  `firstname`,  `email`, `telephone`,  `payment_address_1`,  `payment_city`, `payment_postcode`, `payment_country`, `payment_zone`, `comment`, `total`,`order_status_id`, `currency_code`, `currency_value`, `ip`, `date_added`, `date_modified`) 
						VALUES ('".$strs[0]."','".$strs[4]."','".$strs[5].' '.$strs[6]."','".$strs[7]."','".$strs[8]."','".$strs[11]."','','".$strs[12]."','".$strs[13]."','','".$strs[18]."','".$strs[19]."','1','".$strs[26]."','".$strs[27]."','".$strs[28]."','".$strs[32]."','".$strs[33]."')");
				      $ordenum=$ordenum+1;
					}
				
				}elseif($strsfirst[4]=='网站地址'&&$strsfirst[13]=='地区'){
					$order_query = $this->db->query("SELECT order_id FROM " . DB_PREFIX . "order o WHERE o.order_number = '" . (int)$strs[0] . "'");
					if(!$order_query->num_rows){
						$this->db->query("INSERT INTO `oc_order` ( `order_number`,`store_url`,  `firstname`,  `email`, `telephone`,  `payment_address_1`,  `payment_city`, `payment_postcode`, `payment_country`, `payment_zone`, `comment`, `total`,`order_status_id`, `currency_code`, `currency_value`, `ip`, `date_added`, `date_modified`) 
						VALUES ('".$strs[0]."','".$strs[4]."','".$strs[5].' '.$strs[6]."','".$strs[7]."','".$strs[8]."','".$strs[11]."','".$strs[14]."','".$strs[12]."','台湾','".$strs[15]."','".$strs[20]."','".$strs[21]."','1','".$strs[28]."','".$strs[29]."','".$strs[30]."','".$strs[34]."','".$strs[35]."')");
				        $ordenum=$ordenum+1;
					}
					
					
				}elseif($strsfirsts==$check_one&&$strs[0]){
					
					// var_dump($strs[19]);
					// var_dump($strs[20]);
					$order_query = $this->db->query("SELECT order_number FROM " . DB_PREFIX . "order o WHERE o.order_number = '" . (int)$strs[0] . "'");
					if(!$order_query->num_rows){
						
						$this->db->query("INSERT INTO `oc_order` ( `ad_person`,`department`, `order_number`,  `product_name`, `in_name`, `out_name`,  `sku`, `num`, `price`,  `verification_status`,   `store_url`,  `firstname`,  `email`, `telephone`,  `payment_address_1`,  `payment_city`, `payment_postcode`, `payment_country`, `payment_zone`, `comment`, `total`,`order_status_id`,`currency_code`,  `date_added`, `ontheground`, `rmb`) 
						VALUES ('".$strs[3]."','".$strs[2]."','".$strs[0]."','".$strs[24]."','".$strs[24]."','".$strs[25]."','".$strs[23]."','".$strs[28]."','".$strs[27]."','".$strs[5]."','".$strs[4]."','".$strs[6]."','".$strs[12]."','".$strs[14]."','".$strs[11]."','".$strs[9]."','".$strs[12]."','".$strs[7]."','".$strs[10]."','".$strs[16]."','".$strs[19]."','1','".$strs[18]."','".$strs[1]."','".$strs[17]."','".$strs[20]."')");
					    $leng=count($strsfirst);
						$j=$leng-30;
						$i=0;
						$m=$j/6;
						for($i;$i<$j;$i++){
							$start=29+$i;
							$attr=$start+1;
							$attrv=$start+4;
							// $attribute=array_slice($strsfirst,$start,6);
							if($strs[$attr]&&$strs[$attrv]){
								$this->db->query("INSERT INTO `oc_order_option`( `name`, `value`, `order_number`) 
							    VALUES ('".$strs[$attr]."','".$strs[$attrv]."','".$strs[0]."')");
							}
							
							$i=$i+5;
						}
						$ordenum=$ordenum+1;
					}				
				}elseif($strsfirst==$check_two&&$strs[4]){
																																																											
					
					$order_query = $this->db->query("SELECT order_number FROM " . DB_PREFIX . "order o WHERE o.order_number = '" . (int)$strs[4] . "'");
					if(!$order_query->num_rows){
						 // var_dump($strs);
						 // var_dump($strsfirst);
						 if($strs[34]=='已签收'){
							 $strs[34]=17;
						 }elseif($strs[34]=='待采购'){
							 $strs[34]=18;
						 }elseif($strs[34]=='待发货'){
							 $strs[34]=19;
						 }elseif($strs[34]=='待指定物流'){
							 $strs[34]=20;
						 }elseif($strs[34]=='拒收'){
							 $strs[34]=21;
						 }elseif($strs[34]=='无效订单'){
							 $strs[34]=22;
						 }elseif($strs[34]=='已发货'){
							 $strs[34]=23;
						 }elseif($strs[34]=='已取消'){
							 $strs[34]=24;
						 }
						 
						 $check=array(
							0 =>'广告专员',1 =>'所属业务部门' ,2 =>'域名' ,3 => '订单号' , 4 => '商户订单号' , 5 =>'运单号',6 =>'姓名' , 7 =>'产品名' , 8 =>'内部名', 9 =>'面单名' ,10 =>'总价' ,11 =>'符号',12 =>'属性' ,13 =>'SKU' ,14 =>'电话', 15 =>'邮箱', 16 =>'邮编' , 17 =>'送货地址' , 18 =>'省',19 =>'市' ,20 =>'区' ,21 =>'购买产品数量' ,22 =>'单价', 23 =>'付款方式', 24 =>'来源',25 =>'留言',26 =>'同步时间',27 =>'下单时间' ,28 =>'到货时间' ,29 =>'上线时间',30 =>'签收时间', 31 =>'仓库', 32 =>'订单状态',33 =>'无效原因' ,34 =>'取消原因', 35 =>'验证码状态',36 =>'发货日期',37 =>'物流名称' ,38 =>'物流状态' ,39 =>'结款状态' ,40 =>'重量', 41 =>'IP' ,42 =>'重复信息' 
						);

						
						$check_two=array(
							0 =>'地区',1 =>'广告专员',2 =>'所属业务部门',3 =>'域名',4 =>'订单号',5 =>'商户订单号',6 =>'运单号',7 =>'姓名',8 =>'内部名',9 =>'面单名',10 =>'总价',11 =>'人民币',12 =>'符号',13 =>'属性',14 =>'SKU',15 =>'电话',16 =>'邮箱',17 =>'邮编',18 =>'送货地址',19 =>'省',20 =>'市',21 =>'区',22 =>'留言',23 =>'购买产品数量',24 =>'单价',25 =>'付款方式',26 =>'来源',27 =>'备注',28 =>'同步时间',29 =>'下单时间',30 =>'到货时间',31 =>'上线时间',32 =>'签收时间',33 =>'仓库',34 =>'订单状态',35 =>'分类',36 =>'无效原因',37 =>'取消原因',38 =>'验证码状态',39 =>'发货日期',40 =>'物流名称',41 =>'物流状态',42 =>'结款状态',43 =>'重量',44 =>'标签'
						);
						 
						 
						 $this->db->query("INSERT INTO `oc_order` ( `ad_person`,`department`, `order_number`, `out_trade_no`, `tracking_number`, `in_name`, `out_name`, `attribute`, `sku`, `num`, `price`, `arrival_time`, `sign_time`, `warehouse`, `cause_invalidity`, `verification_status`, `delivery_date`, `logistics_name`, `logistics_status`, `wight`, `duplicate_message`, `store_url`,  `firstname`,  `email`, `telephone`,  `payment_address_1`,  `payment_city`, `payment_postcode`, `payment_country`, `payment_zone`, `comment`, `total`,`order_status_id`, `currency_code`, `currency_value`, `date_added`, `date_modified`, `ontheground`, `rmb`) 
						VALUES ('".$strs[1]."','".$strs[2]."','".$strs[4]."','".$strs[5]."','".$strs[6]."','".$strs[8]."','".$strs[9]."','".$strs[13]."','".$strs[14]."','".$strs[23]."','".$strs[24]."','".$strs[30]."','".$strs[32]."','".$strs[33]."','".$strs[36]."','".$strs[38]."','".$strs[36]."','".$strs[37]."','".$strs[38]."','".$strs[43]."','".$strs[44]."','".$strs[3]."','".$this->db->escape($strs[7])."','".$strs[16]."','".$strs[15]."','".$this->db->escape($strs[18])."','".$strs[20]."','".$strs[16]."','".$strs[19]."','".$strs[21]."','".$strs[22]."','".$strs[10]."','".$strs[34]."','".$strs[12]."','1','".$strs[29]."','".$strs[28]."','".$strs[0]."','".$strs[11]."')");
					   $ordenum=$ordenum+1;
					}				
				}elseif($strsfirst==$check&&$strs[3]){
					$order_query = $this->db->query("SELECT order_number FROM " . DB_PREFIX . "order o WHERE o.order_number = '" . (int)$strs[3] . "'");
					if(!$order_query->num_rows){
						 // var_dump($strs);
						 // var_dump($strsfirst);
						 if($strs[32]=='已签收'){
							 $strs[32]=17;
						 }elseif($strs[32]=='待采购'){
							 $strs[32]=18;
						 }elseif($strs[32]=='待发货'){
							 $strs[32]=19;
						 }elseif($strs[32]=='待指定物流'){
							 $strs[32]=20;
						 }elseif($strs[32]=='拒收'){
							 $strs[32]=21;
						 }elseif($strs[32]=='无效订单'){
							 $strs[32]=22;
						 }elseif($strs[32]=='已发货'){
							 $strs[32]=23;
						 }elseif($strs[32]=='已取消'){
							 $strs[32]=24;
						 }
						 $this->db->query("INSERT INTO `oc_order` ( `ad_person`,`department`, `order_number`, `out_trade_no`, `tracking_number`, `product_name`, `in_name`, `out_name`, `attribute`, `sku`, `num`, `price`, `arrival_time`, `sign_time`, `warehouse`, `cause_invalidity`, `verification_status`, `delivery_date`, `logistics_name`, `logistics_status`, `wight`, `duplicate_message`, `store_url`,  `firstname`,  `email`, `telephone`,  `payment_address_1`,  `payment_city`, `payment_postcode`, `payment_country`, `payment_zone`, `comment`, `total`,`order_status_id`, `currency_code`, `currency_value`, `ip`, `date_added`, `date_modified`) 
						VALUES ('".$strs[0]."','".$strs[1]."','".$strs[3]."','".$strs[4]."','".$strs[5]."','".$strs[7]."','".$strs[8]."','".$strs[9]."','".$strs[12]."','".$strs[13]."','".$strs[21]."','".$strs[22]."','".$strs[28]."','".$strs[30]."','".$strs[31]."','".$strs[33]."','".$strs[35]."','".$strs[36]."','".$strs[37]."','".$strs[38]."','".$strs[40]."','".$strs[42]."','".$strs[2]."','".$this->db->escape($strs[6])."','".$strs[15]."','".$strs[14]."','".$this->db->escape($strs[16])."','".$strs[19]."','".$strs[16]."','".$strs[18]."','".$strs[20]."','".$strs[25]."','".$strs[10]."','".$strs[32]."','".$strs[11]."','1','".$this->db->escape($strs[41])."','".$strs[27]."','".$strs[26]."')");
					    $ordenum=$ordenum+1;
					}				
				}
			
			    sleep(0.5);
			
			
			
		}
     echo "成功导入订单".$ordenum."条";
     die;

		
	}
	
	public function getStatisticsOrders() {
		 $statistics_query = $this->db->query("select date_format( o.date_added, '%Y-%m-%d' ) as 'date',count(o.order_id)  as 'subtotal',
												(select count(oc.cause_invalidity)  from  " . DB_PREFIX . "order oc WHERE  date_format( oc.date_added, '%Y-%m-%d' )=date_format( o.date_added, '%Y-%m-%d' ) AND oc.cause_invalidity='有效订单' GROUP BY  date_format( oc.date_added, '%Y-%m-%d' )) as 'effective', 
												(select count(oc.cause_invalidity)  from  " . DB_PREFIX . "order oc WHERE  date_format( oc.date_added, '%Y-%m-%d' )=date_format( o.date_added, '%Y-%m-%d' ) AND oc.order_status_id='17' GROUP BY  date_format( oc.date_added, '%Y-%m-%d' )) as 'signed' ,
												(select count(oc.cause_invalidity)  from  " . DB_PREFIX . "order oc WHERE  date_format( oc.date_added, '%Y-%m-%d' )=date_format( o.date_added, '%Y-%m-%d' ) AND oc.order_status_id='18' GROUP BY  date_format( oc.date_added, '%Y-%m-%d' )) as 'purchase', 
												(select count(oc.cause_invalidity)  from  " . DB_PREFIX . "order oc WHERE  date_format( oc.date_added, '%Y-%m-%d' )=date_format( o.date_added, '%Y-%m-%d' ) AND oc.order_status_id='19' GROUP BY  date_format( oc.date_added, '%Y-%m-%d' )) as 'tosend' ,
												(select count(oc.cause_invalidity)  from  " . DB_PREFIX . "order oc WHERE  date_format( oc.date_added, '%Y-%m-%d' )=date_format( o.date_added, '%Y-%m-%d' ) AND oc.order_status_id='20' GROUP BY  date_format( oc.date_added, '%Y-%m-%d' )) as 'designated' ,
												(select count(oc.cause_invalidity)  from  " . DB_PREFIX . "order oc WHERE  date_format( oc.date_added, '%Y-%m-%d' )=date_format( o.date_added, '%Y-%m-%d' ) AND oc.order_status_id='23' GROUP BY  date_format( oc.date_added, '%Y-%m-%d' )) as 'shipped' ,
												(select count(oc.cause_invalidity)  from  " . DB_PREFIX . "order oc WHERE  date_format( oc.date_added, '%Y-%m-%d' )=date_format( o.date_added, '%Y-%m-%d' ) AND oc.order_status_id='21' GROUP BY  date_format( oc.date_added, '%Y-%m-%d' )) as 'rejection' 
												from  " . DB_PREFIX . "order o GROUP BY  date_format( o.date_added, '%Y-%m-%d' ) ORDER BY date_format( o.date_added, '%Y-%m-%d' ) DESC");
	    
		return  $statistics_query->rows;
	}
	public function getOrder($order_id) {
		$order_query = $this->db->query("SELECT *, (SELECT CONCAT(c.firstname, ' ', c.lastname) FROM " . DB_PREFIX . "customer c WHERE c.customer_id = o.customer_id) AS customer, (SELECT os.name FROM " . DB_PREFIX . "order_status os WHERE os.order_status_id = o.order_status_id AND os.language_id = '" . (int)$this->config->get('config_language_id') . "') AS order_status FROM `" . DB_PREFIX . "order` o WHERE o.order_id = '" . (int)$order_id . "'");

		if ($order_query->num_rows) {
			$country_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "country` WHERE country_id = '" . (int)$order_query->row['payment_country_id'] . "'");

			if ($country_query->num_rows) {
				$payment_iso_code_2 = $country_query->row['iso_code_2'];
				$payment_iso_code_3 = $country_query->row['iso_code_3'];
			} else {
				$payment_iso_code_2 = '';
				$payment_iso_code_3 = '';
			}

			$zone_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "zone` WHERE zone_id = '" . (int)$order_query->row['payment_zone_id'] . "'");

			if ($zone_query->num_rows) {
				$payment_zone_code = $zone_query->row['code'];
			} else {
				$payment_zone_code = '';
			}

			$country_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "country` WHERE country_id = '" . (int)$order_query->row['shipping_country_id'] . "'");

			if ($country_query->num_rows) {
				$shipping_iso_code_2 = $country_query->row['iso_code_2'];
				$shipping_iso_code_3 = $country_query->row['iso_code_3'];
			} else {
				$shipping_iso_code_2 = '';
				$shipping_iso_code_3 = '';
			}

			$zone_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "zone` WHERE zone_id = '" . (int)$order_query->row['shipping_zone_id'] . "'");

			if ($zone_query->num_rows) {
				$shipping_zone_code = $zone_query->row['code'];
			} else {
				$shipping_zone_code = '';
			}

			$reward = 0;

			$order_product_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_product WHERE order_id = '" . (int)$order_id . "'");

			foreach ($order_product_query->rows as $product) {
				$reward += $product['reward'];
			}
			
			$this->load->model('customer/customer');

			$affiliate_info = $this->model_customer_customer->getCustomer($order_query->row['affiliate_id']);

			if ($affiliate_info) {
				$affiliate_firstname = $affiliate_info['firstname'];
				$affiliate_lastname = $affiliate_info['lastname'];
			} else {
				$affiliate_firstname = '';
				$affiliate_lastname = '';
			}

			$this->load->model('localisation/language');

			$language_info = $this->model_localisation_language->getLanguage($order_query->row['language_id']);

			if ($language_info) {
				$language_code = $language_info['code'];
			} else {
				$language_code = $this->config->get('config_language');
			}

			return array(
				'order_id'                => $order_query->row['order_id'],
				'invoice_no'              => $order_query->row['invoice_no'],
				'invoice_prefix'          => $order_query->row['invoice_prefix'],
				'store_id'                => $order_query->row['store_id'],
				'store_name'              => $order_query->row['store_name'],
				'store_url'               => $order_query->row['store_url'],
				'customer_id'             => $order_query->row['customer_id'],
				'customer'                => $order_query->row['customer'],
				'customer_group_id'       => $order_query->row['customer_group_id'],
				'firstname'               => $order_query->row['firstname'],
				'lastname'                => $order_query->row['lastname'],
				'email'                   => $order_query->row['email'],
				'telephone'               => $order_query->row['telephone'],
				'custom_field'            => json_decode($order_query->row['custom_field'], true),
				'payment_firstname'       => $order_query->row['payment_firstname'],
				'payment_lastname'        => $order_query->row['payment_lastname'],
				'payment_company'         => $order_query->row['payment_company'],
				'payment_address_1'       => $order_query->row['payment_address_1'],
				'payment_address_2'       => $order_query->row['payment_address_2'],
				'payment_postcode'        => $order_query->row['payment_postcode'],
				'payment_city'            => $order_query->row['payment_city'],
				'payment_zone_id'         => $order_query->row['payment_zone_id'],
				'payment_zone'            => $order_query->row['payment_zone'],
				'payment_zone_code'       => $payment_zone_code,
				'payment_country_id'      => $order_query->row['payment_country_id'],
				'payment_country'         => $order_query->row['payment_country'],
				'payment_iso_code_2'      => $payment_iso_code_2,
				'payment_iso_code_3'      => $payment_iso_code_3,
				'payment_address_format'  => $order_query->row['payment_address_format'],
				'payment_custom_field'    => json_decode($order_query->row['payment_custom_field'], true),
				'payment_method'          => $order_query->row['payment_method'],
				'payment_code'            => $order_query->row['payment_code'],
				'shipping_firstname'      => $order_query->row['shipping_firstname'],
				'shipping_lastname'       => $order_query->row['shipping_lastname'],
				'shipping_company'        => $order_query->row['shipping_company'],
				'shipping_address_1'      => $order_query->row['shipping_address_1'],
				'shipping_address_2'      => $order_query->row['shipping_address_2'],
				'shipping_postcode'       => $order_query->row['shipping_postcode'],
				'shipping_city'           => $order_query->row['shipping_city'],
				'shipping_zone_id'        => $order_query->row['shipping_zone_id'],
				'shipping_zone'           => $order_query->row['shipping_zone'],
				'shipping_zone_code'      => $shipping_zone_code,
				'shipping_country_id'     => $order_query->row['shipping_country_id'],
				'shipping_country'        => $order_query->row['shipping_country'],
				'shipping_iso_code_2'     => $shipping_iso_code_2,
				'shipping_iso_code_3'     => $shipping_iso_code_3,
				'shipping_address_format' => $order_query->row['shipping_address_format'],
				'shipping_custom_field'   => json_decode($order_query->row['shipping_custom_field'], true),
				'shipping_method'         => $order_query->row['shipping_method'],
				'shipping_code'           => $order_query->row['shipping_code'],
				'comment'                 => $order_query->row['comment'],
				'total'                   => $order_query->row['total'],
				'reward'                  => $reward,
				'order_status_id'         => $order_query->row['order_status_id'],
				'order_status'            => $order_query->row['order_status'],
				'affiliate_id'            => $order_query->row['affiliate_id'],
				'affiliate_firstname'     => $affiliate_firstname,
				'affiliate_lastname'      => $affiliate_lastname,
				'commission'              => $order_query->row['commission'],
				'language_id'             => $order_query->row['language_id'],
				'language_code'           => $language_code,
				'currency_id'             => $order_query->row['currency_id'],
				'currency_code'           => $order_query->row['currency_code'],
				'currency_value'          => $order_query->row['currency_value'],
				'ip'                      => $order_query->row['ip'],
				'forwarded_ip'            => $order_query->row['forwarded_ip'],
				'user_agent'              => $order_query->row['user_agent'],
				'accept_language'         => $order_query->row['accept_language'],
				'date_added'              => $order_query->row['date_added'],
				'date_modified'           => $order_query->row['date_modified']
			);
		} else {
			return;
		}
	}
	
	public function getOrderAttribute($order_number) {
		$order_query = $this->db->query("SELECT order_number,name,value FROM `" . DB_PREFIX . "order_option` op WHERE op.order_number = '" . $order_number . "'");
        if ($order_query->rows) {
			return $order_query->rows;
		}
		
	}
	
    // order_status
	public function getOrders($data = array()) {
		$sql = "SELECT o.order_id,o.payment_zone,o.payment_city,o.remark,o.payment_address_1,o.order_number,o.store_url,o.email,o.telephone,o.payment_address_1,o.firstname,o.out_trade_no,o.tracking_number,o.product_name,o.in_name,o.payment_country,o.ad_person,o.ip,out_name,o.attribute,o.sku,o.num,o.price,o.arrival_time,o.sign_time,o.warehouse,o.cause_invalidity,o.verification_status,o.delivery_date,o.logistics_name,o.logistics_status,o.wight,o.duplicate_message,o.comment,o.total,o.order_status_id, CONCAT(o.firstname, ' ', o.lastname) AS customer, (SELECT os.name FROM " . DB_PREFIX . "order_status os WHERE os.order_status_id = o.order_status_id AND os.language_id = '" . (int)$this->config->get('config_language_id') . "') AS order_status,(SELECT os.name FROM " . DB_PREFIX . "order_status os WHERE os.order_status_id = o.cause_invalidity AND os.language_id = '" . (int)$this->config->get('config_language_id') . "') AS cause_invalidity_name, o.shipping_code, o.total, o.currency_code, o.currency_value, o.date_added, o.date_modified FROM `" . DB_PREFIX . "order` o";

		if (!empty($data['filter_order_status'])) {
			$implode = array();

			$order_statuses = explode(',', $data['filter_order_status']);

			foreach ($order_statuses as $order_status_id) {
				$implode[] = "o.order_status_id = '" . (int)$order_status_id . "'";
			}

			if ($implode) {
				$sql .= " WHERE (" . implode(" OR ", $implode) . ")";
			}
		} elseif (isset($data['filter_order_status_id']) && $data['filter_order_status_id'] !== '') {
			$sql .= " WHERE o.order_status_id = '" . (int)$data['filter_order_status_id'] . "'";
		} else {
			$sql .= " WHERE o.order_status_id >= '0'";
		}

		if (!empty($data['filter_order_id'])) {
			$sql .= " AND o.order_number = '" . (int)$data['filter_order_id'] . "'";
		}

		if (!empty($data['filter_customer'])) {
			$sql .= " AND CONCAT(o.firstname, ' ', o.lastname) LIKE '%" . $this->db->escape($data['filter_customer']) . "%'";
		}

		if (!empty($data['filter_date_added'])) {
			$sql .= " AND DATE(o.date_added) = DATE('" . $this->db->escape($data['filter_date_added']) . "')";
		}

		if (!empty($data['filter_date_modified'])) {
			$sql .= " AND DATE(o.date_modified) = DATE('" . $this->db->escape($data['filter_date_modified']) . "')";
		}

		if (!empty($data['filter_total'])) {
			$sql .= " AND o.total = '" . (float)$data['filter_total'] . "'";
		}
		
		if (!empty($data['telephone'])) {
			$sql .= " AND o.telephone = '" . $data['telephone'] . "'";
		}
		
		if (!empty($data['email'])) {
			$sql .= " AND o.email = '" . $data['email'] . "'";
		}

		$sort_data = array(
			'o.order_id',
			'customer',
			'order_status',
			'o.date_added',
			'o.date_modified',
			'o.total'
		);

		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
			$sql .= " ORDER BY " . $data['sort'];
		} else {
			$sql .= " ORDER BY o.order_id";
		}

		if (isset($data['order']) && ($data['order'] == 'DESC')) {
			$sql .= " DESC";
		} else {
			$sql .= " ASC";
		}

		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}

			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}

			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}
		$query = $this->db->query($sql);

		return $query->rows;
	}
	
	public function getrepeatipOrders($data = array()) {
		$sql = "SELECT count(*) as num,ip FROM `" . DB_PREFIX . "order` o";

		if (!empty($data['filter_order_status'])) {
			$implode = array();

			$order_statuses = explode(',', $data['filter_order_status']);

			foreach ($order_statuses as $order_status_id) {
				$implode[] = "o.order_status_id = '" . (int)$order_status_id . "'";
			}

			if ($implode) {
				$sql .= " WHERE (" . implode(" OR ", $implode) . ")";
			}
		} elseif (isset($data['filter_order_status_id']) && $data['filter_order_status_id'] !== '') {
			$sql .= " WHERE o.order_status_id = '" . (int)$data['filter_order_status_id'] . "'";
		} else {
			$sql .= " WHERE o.order_status_id >= '0'";
		}

		if (!empty($data['filter_order_id'])) {
			$sql .= " AND o.order_number = '" . (int)$data['filter_order_id'] . "'";
		}

		if (!empty($data['filter_customer'])) {
			$sql .= " AND CONCAT(o.firstname, ' ', o.lastname) LIKE '%" . $this->db->escape($data['filter_customer']) . "%'";
		}

		if (!empty($data['filter_date_added'])) {
			$sql .= " AND DATE(o.date_added) = DATE('" . $this->db->escape($data['filter_date_added']) . "')";
		}

		if (!empty($data['filter_date_modified'])) {
			$sql .= " AND DATE(o.date_modified) = DATE('" . $this->db->escape($data['filter_date_modified']) . "')";
		}

		if (!empty($data['filter_total'])) {
			$sql .= " AND o.total = '" . (float)$data['filter_total'] . "'";
		}

		

			$sql .= "  GROUP BY ip HAVING num > 1";
		
		$query = $this->db->query($sql);

		return $query->rows;
	}
	
	public function getrepeataddOrders($data = array()) {
		$sql = "SELECT count(*) as num,payment_address_1 FROM `" . DB_PREFIX . "order` o";

		if (!empty($data['filter_order_status'])) {
			$implode = array();

			$order_statuses = explode(',', $data['filter_order_status']);

			foreach ($order_statuses as $order_status_id) {
				$implode[] = "o.order_status_id = '" . (int)$order_status_id . "'";
			}

			if ($implode) {
				$sql .= " WHERE (" . implode(" OR ", $implode) . ")";
			}
		} elseif (isset($data['filter_order_status_id']) && $data['filter_order_status_id'] !== '') {
			$sql .= " WHERE o.order_status_id = '" . (int)$data['filter_order_status_id'] . "'";
		} else {
			$sql .= " WHERE o.order_status_id >= '0'";
		}

		if (!empty($data['filter_order_id'])) {
			$sql .= " AND o.order_number = '" . (int)$data['filter_order_id'] . "'";
		}

		if (!empty($data['filter_customer'])) {
			$sql .= " AND CONCAT(o.firstname, ' ', o.lastname) LIKE '%" . $this->db->escape($data['filter_customer']) . "%'";
		}

		if (!empty($data['filter_date_added'])) {
			$sql .= " AND DATE(o.date_added) = DATE('" . $this->db->escape($data['filter_date_added']) . "')";
		}

		if (!empty($data['filter_date_modified'])) {
			$sql .= " AND DATE(o.date_modified) = DATE('" . $this->db->escape($data['filter_date_modified']) . "')";
		}

		if (!empty($data['filter_total'])) {
			$sql .= " AND o.total = '" . (float)$data['filter_total'] . "'";
		}

		

			$sql .= " GROUP BY payment_address_1 HAVING num > 1";
		
		$query = $this->db->query($sql);

		return $query->rows;
	}
	
	public function getrepeatteleOrders($data = array()) {
		$sql = "SELECT count(*) as num,telephone FROM `" . DB_PREFIX . "order` o";

		if (!empty($data['filter_order_status'])) {
			$implode = array();

			$order_statuses = explode(',', $data['filter_order_status']);

			foreach ($order_statuses as $order_status_id) {
				$implode[] = "o.order_status_id = '" . (int)$order_status_id . "'";
			}

			if ($implode) {
				$sql .= " WHERE (" . implode(" OR ", $implode) . ")";
			}
		} elseif (isset($data['filter_order_status_id']) && $data['filter_order_status_id'] !== '') {
			$sql .= " WHERE o.order_status_id = '" . (int)$data['filter_order_status_id'] . "'";
		} else {
			$sql .= " WHERE o.order_status_id >= '0'";
		}

		if (!empty($data['filter_order_id'])) {
			$sql .= " AND o.order_number = '" . (int)$data['filter_order_id'] . "'";
		}

		if (!empty($data['filter_customer'])) {
			$sql .= " AND CONCAT(o.firstname, ' ', o.lastname) LIKE '%" . $this->db->escape($data['filter_customer']) . "%'";
		}

		if (!empty($data['filter_date_added'])) {
			$sql .= " AND DATE(o.date_added) = DATE('" . $this->db->escape($data['filter_date_added']) . "')";
		}

		if (!empty($data['filter_date_modified'])) {
			$sql .= " AND DATE(o.date_modified) = DATE('" . $this->db->escape($data['filter_date_modified']) . "')";
		}

		if (!empty($data['filter_total'])) {
			$sql .= " AND o.total = '" . (float)$data['filter_total'] . "'";
		}

		

			$sql .= " GROUP BY telephone HAVING num > 1";
		
		$query = $this->db->query($sql);

		return $query->rows;
	}
	
    public function getrepeatemailOrders($data = array()) {
		$sql = "SELECT count(*) as num,email FROM `" . DB_PREFIX . "order` o";

		if (!empty($data['filter_order_status'])) {
			$implode = array();

			$order_statuses = explode(',', $data['filter_order_status']);

			foreach ($order_statuses as $order_status_id) {
				$implode[] = "o.order_status_id = '" . (int)$order_status_id . "'";
			}

			if ($implode) {
				$sql .= " WHERE (" . implode(" OR ", $implode) . ")";
			}
		} elseif (isset($data['filter_order_status_id']) && $data['filter_order_status_id'] !== '') {
			$sql .= " WHERE o.order_status_id = '" . (int)$data['filter_order_status_id'] . "'";
		} else {
			$sql .= " WHERE o.order_status_id >= '0'";
		}

		if (!empty($data['filter_order_id'])) {
			$sql .= " AND o.order_number = '" . (int)$data['filter_order_id'] . "'";
		}

		if (!empty($data['filter_customer'])) {
			$sql .= " AND CONCAT(o.firstname, ' ', o.lastname) LIKE '%" . $this->db->escape($data['filter_customer']) . "%'";
		}

		if (!empty($data['filter_date_added'])) {
			$sql .= " AND DATE(o.date_added) = DATE('" . $this->db->escape($data['filter_date_added']) . "')";
		}

		if (!empty($data['filter_date_modified'])) {
			$sql .= " AND DATE(o.date_modified) = DATE('" . $this->db->escape($data['filter_date_modified']) . "')";
		}

		if (!empty($data['filter_total'])) {
			$sql .= " AND o.total = '" . (float)$data['filter_total'] . "'";
		}

		

			$sql .= " GROUP BY email HAVING num > 1";
		
		$query = $this->db->query($sql);

		return $query->rows;
	}
	
	public function getrepeatefirstOrders($data = array()) {
		$sql = "SELECT count(*) as num,firstname FROM `" . DB_PREFIX . "order` o";

		if (!empty($data['filter_order_status'])) {
			$implode = array();

			$order_statuses = explode(',', $data['filter_order_status']);

			foreach ($order_statuses as $order_status_id) {
				$implode[] = "o.order_status_id = '" . (int)$order_status_id . "'";
			}

			if ($implode) {
				$sql .= " WHERE (" . implode(" OR ", $implode) . ")";
			}
		} elseif (isset($data['filter_order_status_id']) && $data['filter_order_status_id'] !== '') {
			$sql .= " WHERE o.order_status_id = '" . (int)$data['filter_order_status_id'] . "'";
		} else {
			$sql .= " WHERE o.order_status_id >= '0'";
		}

		if (!empty($data['filter_order_id'])) {
			$sql .= " AND o.order_number = '" . (int)$data['filter_order_id'] . "'";
		}

		if (!empty($data['filter_customer'])) {
			$sql .= " AND CONCAT(o.firstname, ' ', o.lastname) LIKE '%" . $this->db->escape($data['filter_customer']) . "%'";
		}

		if (!empty($data['filter_date_added'])) {
			$sql .= " AND DATE(o.date_added) = DATE('" . $this->db->escape($data['filter_date_added']) . "')";
		}

		if (!empty($data['filter_date_modified'])) {
			$sql .= " AND DATE(o.date_modified) = DATE('" . $this->db->escape($data['filter_date_modified']) . "')";
		}

		if (!empty($data['filter_total'])) {
			$sql .= " AND o.total = '" . (float)$data['filter_total'] . "'";
		}

		

			$sql .= " GROUP BY firstname HAVING num > 1";
		
		$query = $this->db->query($sql);

		return $query->rows;
	}

	public function getOrderProducts($order_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_product WHERE order_id = '" . (int)$order_id . "'");

		return $query->rows;
	}

	public function getOrderOptions($order_id, $order_product_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_option WHERE order_id = '" . (int)$order_id . "' AND order_product_id = '" . (int)$order_product_id . "'");

		return $query->rows;
	}

	public function getOrderVouchers($order_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_voucher WHERE order_id = '" . (int)$order_id . "'");

		return $query->rows;
	}

	public function getOrderVoucherByVoucherId($voucher_id) {
		$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order_voucher` WHERE voucher_id = '" . (int)$voucher_id . "'");

		return $query->row;
	}

	public function getOrderTotals($order_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_total WHERE order_id = '" . (int)$order_id . "' ORDER BY sort_order");

		return $query->rows;
	}
	
	public function getTotalOrders($data = array()) {
		$sql = "SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "order`";

		if (!empty($data['filter_order_status'])) {
			$implode = array();

			$order_statuses = explode(',', $data['filter_order_status']);

			foreach ($order_statuses as $order_status_id) {
				$implode[] = "order_status_id = '" . (int)$order_status_id . "'";
			}

			if ($implode) {
				$sql .= " WHERE (" . implode(" OR ", $implode) . ")";
			}
		} elseif (isset($data['filter_order_status_id']) && $data['filter_order_status_id'] !== '') {
			$sql .= " WHERE order_status_id = '" . (int)$data['filter_order_status_id'] . "'";
		} else {
			$sql .= " WHERE order_status_id >='0'";
		}

		if (!empty($data['filter_order_id'])) {
			$sql .= " AND order_id = '" . (int)$data['filter_order_id'] . "'";
		}

		if (!empty($data['filter_customer'])) {
			$sql .= " AND CONCAT(firstname, ' ', lastname) LIKE '%" . $this->db->escape($data['filter_customer']) . "%'";
		}

		if (!empty($data['filter_date_added'])) {
			$sql .= " AND DATE(date_added) = DATE('" . $this->db->escape($data['filter_date_added']) . "')";
		}

		if (!empty($data['filter_date_modified'])) {
			$sql .= " AND DATE(date_modified) = DATE('" . $this->db->escape($data['filter_date_modified']) . "')";
		}

		if (!empty($data['filter_total'])) {
			$sql .= " AND total = '" . (float)$data['filter_total'] . "'";
		}

		$query = $this->db->query($sql);

		return $query->row['total'];
	}

	public function getTotalOrdersByStoreId($store_id) {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "order` WHERE store_id = '" . (int)$store_id . "'");

		return $query->row['total'];
	}

	public function getTotalOrdersByOrderStatusId($order_status_id) {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "order` WHERE order_status_id = '" . (int)$order_status_id . "' AND order_status_id > '0'");

		return $query->row['total'];
	}

	public function getTotalOrdersByProcessingStatus() {
		$implode = array();

		$order_statuses = $this->config->get('config_processing_status');

		foreach ($order_statuses as $order_status_id) {
			$implode[] = "order_status_id = '" . (int)$order_status_id . "'";
		}

		if ($implode) {
			$query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "order` WHERE " . implode(" OR ", $implode));

			return $query->row['total'];
		} else {
			return 0;
		}
	}

	public function getTotalOrdersByCompleteStatus() {
		$implode = array();

		$order_statuses = $this->config->get('config_complete_status');

		foreach ($order_statuses as $order_status_id) {
			$implode[] = "order_status_id = '" . (int)$order_status_id . "'";
		}

		if ($implode) {
			$query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "order` WHERE " . implode(" OR ", $implode) . "");

			return $query->row['total'];
		} else {
			return 0;
		}
	}

	public function getTotalOrdersByLanguageId($language_id) {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "order` WHERE language_id = '" . (int)$language_id . "' AND order_status_id > '0'");

		return $query->row['total'];
	}

	public function getTotalOrdersByCurrencyId($currency_id) {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "order` WHERE currency_id = '" . (int)$currency_id . "' AND order_status_id > '0'");

		return $query->row['total'];
	}
	
	public function getTotalSales($data = array()) {
		$sql = "SELECT SUM(total) AS total FROM `" . DB_PREFIX . "order`";

		if (!empty($data['filter_order_status'])) {
			$implode = array();

			$order_statuses = explode(',', $data['filter_order_status']);

			foreach ($order_statuses as $order_status_id) {
				$implode[] = "order_status_id = '" . (int)$order_status_id . "'";
			}

			if ($implode) {
				$sql .= " WHERE (" . implode(" OR ", $implode) . ")";
			}
		} elseif (isset($data['filter_order_status_id']) && $data['filter_order_status_id'] !== '') {
			$sql .= " WHERE order_status_id = '" . (int)$data['filter_order_status_id'] . "'";
		} else {
			$sql .= " WHERE order_status_id > '0'";
		}

		if (!empty($data['filter_order_id'])) {
			$sql .= " AND order_id = '" . (int)$data['filter_order_id'] . "'";
		}

		if (!empty($data['filter_customer'])) {
			$sql .= " AND CONCAT(firstname, ' ', o.lastname) LIKE '%" . $this->db->escape($data['filter_customer']) . "%'";
		}

		if (!empty($data['filter_date_added'])) {
			$sql .= " AND DATE(date_added) = DATE('" . $this->db->escape($data['filter_date_added']) . "')";
		}

		if (!empty($data['filter_date_modified'])) {
			$sql .= " AND DATE(date_modified) = DATE('" . $this->db->escape($data['filter_date_modified']) . "')";
		}

		if (!empty($data['filter_total'])) {
			$sql .= " AND total = '" . (float)$data['filter_total'] . "'";
		}

		$query = $this->db->query($sql);

		return $query->row['total'];
	}
	
	public function createInvoiceNo($order_id) {
		$order_info = $this->getOrder($order_id);

		if ($order_info && !$order_info['invoice_no']) {
			$query = $this->db->query("SELECT MAX(invoice_no) AS invoice_no FROM `" . DB_PREFIX . "order` WHERE invoice_prefix = '" . $this->db->escape($order_info['invoice_prefix']) . "'");

			if ($query->row['invoice_no']) {
				$invoice_no = $query->row['invoice_no'] + 1;
			} else {
				$invoice_no = 1;
			}

			$this->db->query("UPDATE `" . DB_PREFIX . "order` SET invoice_no = '" . (int)$invoice_no . "', invoice_prefix = '" . $this->db->escape($order_info['invoice_prefix']) . "' WHERE order_id = '" . (int)$order_id . "'");

			return $order_info['invoice_prefix'] . $invoice_no;
		}
	}

	public function getOrderHistories($order_id, $start = 0, $limit = 10) {
		if ($start < 0) {
			$start = 0;
		}

		if ($limit < 1) {
			$limit = 10;
		}

		$query = $this->db->query("SELECT oh.date_added, os.name AS status, oh.comment, oh.notify FROM " . DB_PREFIX . "order_history oh LEFT JOIN " . DB_PREFIX . "order_status os ON oh.order_status_id = os.order_status_id WHERE oh.order_id = '" . (int)$order_id . "' AND os.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY oh.date_added DESC LIMIT " . (int)$start . "," . (int)$limit);

		return $query->rows;
	}

	public function getTotalOrderHistories($order_id) {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "order_history WHERE order_id = '" . (int)$order_id . "'");

		return $query->row['total'];
	}

	public function getTotalOrderHistoriesByOrderStatusId($order_status_id) {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "order_history WHERE order_status_id = '" . (int)$order_status_id . "'");

		return $query->row['total'];
	}
	
	public function getEmailsByProductsOrdered($products, $start, $end) {
		$implode = array();

		foreach ($products as $product_id) {
			$implode[] = "op.product_id = '" . (int)$product_id . "'";
		}

		$query = $this->db->query("SELECT DISTINCT email FROM `" . DB_PREFIX . "order` o LEFT JOIN " . DB_PREFIX . "order_product op ON (o.order_id = op.order_id) WHERE (" . implode(" OR ", $implode) . ") AND o.order_status_id <> '0' LIMIT " . (int)$start . "," . (int)$end);

		return $query->rows;
	}

	public function getTotalEmailsByProductsOrdered($products) {
		$implode = array();

		foreach ($products as $product_id) {
			$implode[] = "op.product_id = '" . (int)$product_id . "'";
		}

		$query = $this->db->query("SELECT COUNT(DISTINCT email) AS total FROM `" . DB_PREFIX . "order` o LEFT JOIN " . DB_PREFIX . "order_product op ON (o.order_id = op.order_id) WHERE (" . implode(" OR ", $implode) . ") AND o.order_status_id <> '0'");

		return $query->row['total'];
	}
}
